from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

class CouponPage:
    def __init__(self, driver):
        self.driver = driver
        # 页面元素定位器
        self.coupon_code_input_locator = (By.ID, "coupon-code-input")
        self.apply_coupon_button_locator = (By.ID, "apply-coupon-button")
        self.coupon_status_locator = (By.ID, "coupon-status")
        self.discounted_price_locator = (By.ID, "discounted-price")
        self.total_price_locator = (By.ID, "total-price")
        self.remove_coupon_button_locator = (By.ID, "remove-coupon-button")

    def navigate(self):
        self.driver.get('http://example.com/cart')

    def enter_coupon_code(self, code):
        coupon_code_input = WebDriverWait(self.driver, 10).until(
            EC.presence_of_element_located(self.coupon_code_input_locator)
        )
        coupon_code_input.clear()
        coupon_code_input.send_keys(code)

    def apply_coupon(self):
        apply_coupon_button = WebDriverWait(self.driver, 10).until(
            EC.element_to_be_clickable(self.apply_coupon_button_locator)
        )
        apply_coupon_button.click()

    def get_coupon_status(self):
        coupon_status = WebDriverWait(self.driver, 10).until(
            EC.presence_of_element_located(self.coupon_status_locator)
        )
        return coupon_status.text

    def get_discounted_price(self):
        discounted_price = WebDriverWait(self.driver, 10).until(
            EC.presence_of_element_located(self.discounted_price_locator)
        )
        return discounted_price.text

    def get_total_price(self):
        total_price = WebDriverWait(self.driver, 10).until(
            EC.presence_of_element_located(self.total_price_locator)
        )
        return total_price.text

    def remove_coupon(self):
        remove_coupon_button = WebDriverWait(self.driver, 10).until(
            EC.element_to_be_clickable(self.remove_coupon_button_locator)
        )
        remove_coupon_button.click()

    def is_coupon_applied(self):
        return 'Applied' in self.get_coupon_status()